package com.easylinkin.linkappapi.circuit.controller;

/**
 * @author lyk
 * @date 2025/6/17 下午 8:48
 */

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.circuit.dto.AlarmTypeCount;
import com.easylinkin.linkappapi.circuit.dto.RailGiveSystemAlarmDTO;
import com.easylinkin.linkappapi.circuit.service.IRailGiveSystemAlarmService;
import com.easylinkin.linkappapi.circuit.vo.RailGiveSystemAlarmVO;
import com.easylinkin.linkappapi.common.model.RequestModel;

import java.util.Date;
import java.util.List;

import com.easylinkin.linkappapi.common.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import java.util.Map;
import java.util.Objects;

/**
 * 系统告警信息接口类
 * @author lyk
 * @date 2025/6/17 下午 1:51
 */
@RestController
@RequestMapping("/systemAlarm")
public class RailGiveSystemAlarmController {
    @Autowired
    private IRailGiveSystemAlarmService railGiveSystemAlarmService;

    /**
     * 分页查询
     * @param requestModel
     * @return
     */
    @PostMapping("/getPage")
    public RestMessage selectPage(@RequestBody RequestModel<RailGiveSystemAlarmDTO> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<RailGiveSystemAlarmVO> record = railGiveSystemAlarmService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }
    /**
     * 统计所有告警类型和告警等级统计返回饼图统计接口
     */
   @GetMapping(value = "/totalOldDayByLevelPieChart/{category}")
   public RestMessage totalOldDayByLevelPieChart(@PathVariable("category") String category){
        Map<String,Object> record = railGiveSystemAlarmService.totalOldDayByLevelPieChart(category);
        return RestBuilders.successBuilder(record).build();
   }

    /**
     * 统计指定时间内的告警数据(日,周,月)之内的数据返回以柱状图接口
     * @param dto
     * {
     *   lineDateType://今日0,1近一周(返回7天日期),2近一个月返回30天日期 默认今日0
     * }
     * @return
     */
   @PostMapping(value = "/daysWeekMonthlyByLineChart")
   public RestMessage daysWeekMonthlyByLineChart(@RequestBody RailGiveSystemAlarmDTO dto){
       if (Objects.isNull(dto.getLineDateType())){
           dto.setLineDateType(0);
       }
       Map<String,Object> record = railGiveSystemAlarmService.daysWeekMonthlyByLineChart(dto);
       return RestBuilders.successBuilder(record).build();
   }

  /**
   * 告警分类统计
   *
   * @param category
   * @param typeList
   * @return
   */
   @GetMapping(value = "/alarmTypeCount")
   public RestMessage alarmTypeCount(@RequestParam(value = "category", required = false) String category
                                   ,@RequestParam(value = "typeList", required = false) List<String> typeList){
      List<AlarmTypeCount> alarmTypeCounts = railGiveSystemAlarmService.alarmTypeCount(category,typeList, DateUtil.getTodayStartTime(), DateUtil.getTodayEndTime());
      return RestBuilders.successBuilder(alarmTypeCounts).build();
   }

    /**
     * 时间范围内告警分类统计
     *
     * @param category
     * @param typeList
     * @return
     */
    @GetMapping(value = "/alarmTypeCountWithTime")
    public RestMessage alarmTypeCountWithTime(@RequestParam(value = "category", required = false) String category
            , @RequestParam(value = "typeList", required = false) List<String> typeList
            ,@RequestParam(value = "startDate", required = false)String  startDateStr
            ,@RequestParam(value = "endDate", required = false)String endDateStr){
        Date startDate = DateUtil.parse(startDateStr, "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtil.parse(endDateStr, "yyyy-MM-dd HH:mm:ss");
        List<AlarmTypeCount> alarmTypeCounts = railGiveSystemAlarmService.alarmTypeCount(category,typeList, startDate, endDate);
        return RestBuilders.successBuilder(alarmTypeCounts).build();
    }

    /**
     * 今日告警分类统计
     * @param category
     * @param typeList
     * @return
     */
    @GetMapping(value = "/alarmTypeTodayCount")
    public RestMessage alarmTypeTodayCount(@RequestParam(value = "category", required = false) String category
            ,@RequestParam(value = "typeList", required = false) List<String> typeList){
       //今日告警统计今日情况
        List<AlarmTypeCount> alarmTypeCounts = railGiveSystemAlarmService.alarmTypeCount(category,typeList,DateUtil.getTodayStartTime(),DateUtil.getTodayEndTime());
        return RestBuilders.successBuilder(alarmTypeCounts).build();
    }


    /** 告警处理接口
     * @param dto
     * {
     *     id:"" //记录id
     *     handleFileUrls:""//处理附件
     *     handleResp:""//处理说明
     *     handleRest:0 //0:已处理 1:误报
     * }
     * @return
     */
   @PostMapping(value = "/handle")
   public RestMessage handle(@RequestBody RailGiveSystemAlarmDTO dto){
       if (Objects.isNull(dto.getId())){
           return RestBuilders.failureMessage("记录id不能为空");
       }
       if (Objects.isNull(dto.getHandleRest())){
           return RestBuilders.failureMessage("处理结果不能为空");
       }
       if (!(Integer.valueOf(0).equals(dto.getHandleRest()) || Integer.valueOf(1).equals(dto.getHandleRest()))){
           return RestBuilders.failureMessage("处理结果输入错误");
       }
       railGiveSystemAlarmService.handle(dto);
       return RestBuilders.successMessage("操作成功");
   }

    /**
     * 查询处理告警处理详情接口
     * @param id 记录id
     * @return
     */
   @GetMapping(value = "/getHandleInfo")
   public RestMessage getHandleInfo(String id){
       if (StringUtils.isEmpty(id)){
           return RestBuilders.failureMessage("记录id不能为空");
       }
       Map<String,Object> maps = railGiveSystemAlarmService.getHandleInfo(id);
       if (Objects.isNull(maps)){
           return RestBuilders.failureMessage("当前记录不存在");
       }
       return RestBuilders.successBuilder(maps).build();
   }

}
